package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.Dict;
import com.siwei.mes.mapper.system.DictMapper;
import com.siwei.mes.service.system.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 字典表(Dict)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:20
 */
@Service
public class DictServiceImpl implements DictService {
    @Resource
    private DictMapper dictMapper;

    @Override
    public Long create(Dict entity) {
        dictMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Dict entity) {
        Dict dict = dictMapper.selectByPrimaryKey(entity.getId());
        assertTrue(dict == null, "字典表信息不存在");
        dictMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Dict dict = dictMapper.selectByPrimaryKey(id);
        assertTrue(dict == null, "字典表信息不存在");
        dictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Dict> getPage(PageQuery<Dict, Dict> query) {
        Dict searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> dictMapper.getList(searchParam));
    }

    @Override
    public Dict loadById(Long id) {
        return dictMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Dict> getAll(Dict entity) {
        return dictMapper.getList(entity);
    }
}
